package com.qcl.controller;

import com.qcl.api.PaihaoVO;
import com.qcl.api.ResultVO;
import com.qcl.bean.Paihao;
import com.qcl.bean.PictureInfo;
import com.qcl.repository.PaihaoRepository;
import com.qcl.repository.PictureRepository;
import com.qcl.utils.ApiUtil;
import com.qcl.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序排号相关接口
 */
@RestController
@RequestMapping("/paihao")
public class WxPaihaoController {
    @Autowired
    PaihaoRepository repository;

    //排号接口 type:0小桌，1大桌
    @GetMapping("/quhao")
    public ResultVO quhao(@RequestParam(value = "openid") String openid,
                          @RequestParam(value = "type") Integer type) {
        //条件查询,size代表当前已经排位的最后一个号码
        int size = repository.findByDayAndType(TimeUtils.getYMD(), type).size();
        Paihao paihao = new Paihao();
        paihao.setOpenid(openid);
        paihao.setType(type);
        paihao.setDay(TimeUtils.getYMD());
        paihao.setNum(size + 1);
        paihao.setRuzhuo(false);
        return ApiUtil.success(repository.save(paihao));
    }

    //排号接口 type:0小桌，1大桌
    @GetMapping("/getNum")
    public ResultVO getNum(@RequestParam(value = "openid") String openid) {
        //小桌排到了第几号
        List<Paihao> smallList = repository.findByDayAndTypeAndRuzhuoOrderByNum(TimeUtils.getYMD(), 0, true);
        //小桌排到了第几号
        List<Paihao> bigList = repository.findByDayAndTypeAndRuzhuoOrderByNum(TimeUtils.getYMD(), 1, true);
        int paihaoSmall = 0;
        int paihaobig = 0;
        //当前小桌排到了第几号
        if (smallList != null && smallList.size() > 0) {
            paihaoSmall = smallList.get(smallList.size() - 1).getNum();
        }
        //当前大桌排到了第几号
        if (bigList != null && bigList.size() > 0) {
            paihaobig = bigList.get(bigList.size() - 1).getNum();
        }
        //条件查询,size代表当前已经排位的最后一个号码
        List<Paihao> list = repository.findByDayAndOpenid(TimeUtils.getYMD(), openid);
        Paihao paihao = null;
        if (list != null && list.size() > 0) {
            paihao = list.get(list.size() - 1);
        }
        PaihaoVO paihaoVO = new PaihaoVO();
        paihaoVO.setPaihaoSmall(paihaoSmall);
        paihaoVO.setPaihaoBig(paihaobig);
        paihaoVO.setPaihao(paihao);
        return ApiUtil.success(paihaoVO);
    }

}
